﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Objects.DataClasses;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace DomainModelGenerator
{
    public partial class FormMain : Form
    {
        private const string PathDomain = "{0}\\Domain\\";
        private const string PathService = "{0}\\SqlServer\\";
        private Assembly _assembly = null;
        readonly List<Type> _typeList = new List<Type>();
        public FormMain()
        {
            InitializeComponent();
        }

        private void CmdSelect_Click(object sender, EventArgs e)
        {
            var openFileDialog = new OpenFileDialog {Filter = @"*.dll|*.dll"};
            if(DialogResult.OK == openFileDialog.ShowDialog())
            {
                this.textBoxAssemblyFile.Text = openFileDialog.FileName;
                try
                {
                    _typeList.Clear();
                    _assembly = Assembly.LoadFile(this.textBoxAssemblyFile.Text);
                    Type[] types = _assembly.GetTypes();
                    foreach (var type in types)
                    {
                        if (type.ToString().IndexOf(this.textBoxNamespace.Text) >= 0)
                        {
                            object[] objects = type.GetCustomAttributes(typeof(EdmEntityTypeAttribute), false);
                            if (objects.Count() > 0)
                            {
                                _typeList.Add(type);
                            }
                        }
                    }
                    this.checkedListBoxMain.Items.Clear();
                    this.checkedListBoxMain.Items.AddRange(_typeList.ToArray());
                 
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message, @"错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void GenerateCode()
        {
            
        }

        private void CmdAanlyse_Click(object sender, EventArgs e)
        {
            try
            {
                if(_assembly == null )
                {
                    return;
                }

                if (_typeList.Count  == 0)
                {
                    return;
                }

                var folderBrowserDialog = new FolderBrowserDialog();
                if(DialogResult.OK == folderBrowserDialog.ShowDialog())
                {
                    var folder = folderBrowserDialog.SelectedPath;
                    string pathDomain = string.Format(PathDomain, folder);
                    string pathService = string.Format(PathService, folder);

                     if(!Directory.Exists(pathDomain))
                     {
                         Directory.CreateDirectory(pathDomain);
                     }

                     if (!Directory.Exists(pathService))
                     {
                         Directory.CreateDirectory(pathService);
                     }

                    ICodeGenerator codeGenerator = new CodeGenerator();
                    
                    foreach (var type in this.checkedListBoxMain.CheckedItems)
                    {
                        codeGenerator.GenerateDomainFiles(pathDomain, (Type)type);
                        codeGenerator.GenerateServiceFile(pathService, (Type)type);

                        this.rickAbstract.AppendText(CodeGenerator.LineFeed +
                                                     codeGenerator.GenerateAbstract((Type) type));

                          this.richServiceSqlServer.AppendText(CodeGenerator.LineFeed +
                        codeGenerator.GenerateImpl((Type)type));

                          this.rickIServiceFacade.AppendText(CodeGenerator.LineFeed +
                        codeGenerator.GenerateIServiceFacade((Type)type));

                          this.richServiceFacade.AppendText(CodeGenerator.LineFeed +
                        codeGenerator.GenerateServiceFacade((Type)type));
                    }

                    MessageBox.Show(@"生成成功！路径为：" + folder,@"提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, @"错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void CmdAllSelect_Click(object sender, EventArgs e)
        {
            for(int idx = 0;idx <this.checkedListBoxMain.Items.Count;idx ++)
            {
                this.checkedListBoxMain.SetItemChecked(idx,true);
            }
        }

        private void CmdNotSelect_Click(object sender, EventArgs e)
        {
            for (int idx = 0; idx < this.checkedListBoxMain.Items.Count; idx++)
            {
                this.checkedListBoxMain.SetItemChecked(idx, false);
            }
        }
    }
}
